{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bcb6cdd",
   "metadata": {},
   "source": [
    "# 什么是线程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "408d32ff",
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "43b01978",
   "metadata": {},
   "source": [
    "## 1.2 线程创建与运行\n",
    "三种线程创建方式:\n",
    "- 实现Runnable接口的run方法\n",
    "- 继承Thread类并重写run方法\n",
    "- 使用FutureTask方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e2e01d95",
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "//继承Thread类方式的实现\n",
    "public class ThreadTest {\n",
    "    // 继承Thread类并重写run方法\n",
    "    public static class MyThread extends Thread {\n",
    "\n",
    "        public void run() {\n",
    "            System.out.println(\"I am a child thread.\");\n",
    "            // 输出当前线程的名称\n",
    "            System.out.println(\"当前线程的名称：\" + Thread.currentThread().getName());\n",
    "            // 输出当前线程的ID\n",
    "            System.out.println(\"当前线程的ID：\" + Thread.currentThread().getId());\n",
    "        }\n",
    "    }\n",
    "\n",
    "    public static void main(String[] args) {\n",
    "        // 创建线程对象\n",
    "        MyThread thread = new MyThread();\n",
    "        // 启动线程\n",
    "        thread.start();\n",
    "        \n",
    "        // 输出主线程的信息\n",
    "        System.out.println(\"I am the main thread.\");\n",
    "        System.out.println(\"当前线程的名称：\" + Thread.currentThread().getName());\n",
    "        System.out.println(\"当前线程的ID：\" + Thread.currentThread().getId());\n",
    "    }\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55749341",
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am the main thread.\n"
     ]
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.12+7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
